package com.StarStudio.Controller;

import com.StarStudio.Domain.Expert;
import com.StarStudio.Domain.HttpStatusCode;
import com.StarStudio.Domain.MessageConstant;
import com.StarStudio.Domain.Result;
import com.StarStudio.Service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/experts")
public class ExpertController {

    @Autowired
    ExpertService expertService;

    @GetMapping
    public Result findAll(){
        List<Expert> expertList = null;
        try {
            expertList = expertService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.EXPERT_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_QUERY_SUCCESS, expertList);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable int id){
        Expert byId = null;
        try {
            byId = expertService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.EXPERT_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.EXPERT_QUERY_SUCCESS, byId);
    }


}
